﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.BaoCao.BCBanHangTheoNgay
{
    public partial class frmBanHangTheoNgay : Form
    {
        public frmBanHangTheoNgay()
        {
            InitializeComponent();
            try
            {
                try
                {
                    Entities.CaiDatKho kho = Entitystatic.ViewFile();
                    makho = kho.MaKho;
                    tenkho = kho.TenKho;
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        makho = "";
                        tenkho = "";
                    }
                }
                catch
                {
                }
                date = Entitystatic.date;
                datesosanh = date;
                SelectData();
                loaihoadon = cbbLoai.Text;
                cbbKho.Text = tenkho;
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
                savefile = new System.Windows.Forms.SaveFileDialog();
            }
            catch
            {
            }
        }

        private void frmBanHangTheoNgay_Load(object sender, EventArgs e)
        {
            XuLy();
        }

        private void fix()
        {
            try
            {
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.ReadOnly = true;
                dgvHienThi.Columns["TenHang"].Visible = true;
                dgvHienThi.Columns["DVT"].Visible = true;
                dgvHienThi.Columns["SoLuong"].Visible = true;
                dgvHienThi.Columns["DonGia"].Visible = true;
                dgvHienThi.Columns["KhuyenMai"].Visible = true;
                dgvHienThi.Columns["Thue"].Visible = true;
                dgvHienThi.Columns["GiaTriTT"].Visible = true;
                dgvHienThi.Columns["TenHang"].HeaderText = "Tên Hàng Hóa";
                dgvHienThi.Columns["DVT"].HeaderText = "Đơn Vị Tính";
                dgvHienThi.Columns["SoLuong"].HeaderText = "Số Lượng";
                dgvHienThi.Columns["DonGia"].HeaderText = "Đơn Giá";
                dgvHienThi.Columns["KhuyenMai"].HeaderText = "KM(%)";
                dgvHienThi.Columns["Thue"].HeaderText = "VAT(%)";
                dgvHienThi.Columns["GiaTriTT"].HeaderText = "Thành Tiền";
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dgvHienThi.AllowUserToAddRows = false;
                dgvHienThi.AllowUserToDeleteRows = false;
                dgvHienThi.AllowUserToResizeRows = false;
                dgvHienThi.RowHeadersVisible = false;

            }
            catch
            {
            }
        }

        #region khởi tạo biến
        bool checkClose = false;
        DateTime date;
        DateTime datesosanh;
        Entities.HDBanBuon[] hdbanbuon;
        Entities.HDBanLe[] hdbanle;
        Entities.ChiTietHDBanBuon[] chitiethdbanbuon;
        Entities.ChiTietHDBanle[] chitiethdbanle;
        Entities.KhoHang[] khohang;
        Entities.HangHoa[] hanghoa;
        Entities.DVT[] dvt;
        Entities.GoiHang[] goihang;
        Entities.ChiTietGoiHang[] chitietgoihang;
        ArrayList array;
        ArrayList array1;
        string makho = "";
        string tenkho = "";
        string loaihoadon = "";
        Entities.BCBanHangTheoNgay[] hienthi;
        Entities.BCBanHangTheoNgay[] baocao;
        Entities.BCBanHangTheoNgay save;
        System.Windows.Forms.SaveFileDialog savefile;
        int i = 0;
        #endregion

        void SelectData()
        {
            dvt = Common.StaticVariable.InterfaceObject.SelectDVT(new Entities.DVT("1"));
            if (dvt == null)
                dvt = new Entities.DVT[0];
            hdbanbuon = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();
            if (hdbanbuon == null)
                hdbanbuon = new Entities.HDBanBuon[0];
            chitiethdbanbuon = Common.StaticVariable.InterfaceObject.SelectChiTietHDBanBuon();
            if (chitiethdbanbuon == null)
                chitiethdbanbuon = new Entities.ChiTietHDBanBuon[0];
            hdbanle = Common.StaticVariable.InterfaceObject.SelectHDBanLe();
            if (hdbanle == null)
                hdbanle = new Entities.HDBanLe[0];
            chitiethdbanle = Common.StaticVariable.InterfaceObject.SelectChiTietHDBanLe();
            if (chitiethdbanle == null)
                chitiethdbanle = new Entities.ChiTietHDBanle[0];
            Entities.KhoHang kh1 = new Entities.KhoHang();
            kh1.HanhDong = "1";
            kh1.MaKho = "";
            khohang = Common.StaticVariable.InterfaceObject.SelectKhoHang(kh1);
            if (khohang == null)
                khohang = new Entities.KhoHang[0];
            Entities.ChiTietKhoHang kh = new Entities.ChiTietKhoHang();
            kh.HanhDong = "HangHoaTheoKho";
            kh.MaKho = makho;
            hanghoa = Common.StaticVariable.InterfaceObject.Select_HangHoa(kh);
            if (hanghoa == null)
                hanghoa = new Entities.HangHoa[0];

            Entities.KhoHang[] temp = new Entities.KhoHang[khohang.Length + 1];
            if (temp.Length != 1)
            {
                int sotang = 0;
                foreach (Entities.KhoHang item in khohang)
                {
                    temp[sotang] = item;
                    sotang++;
                }
                temp[temp.Length - 1] = new Entities.KhoHang();
                temp[temp.Length - 1].MaKho = "Tất cả";
                temp[temp.Length - 1].TenKho = "Tất cả";
                Common.Utilities.XuLyComboBox(cbbKho, temp, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
            else
            {
                Common.Utilities.XuLyComboBox(cbbKho, khohang, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
        }

        #region In
        void In()
        {
            if (hienthi.Length - 3 == 0)
            {
                return;
            }
            try
            {
                Entities.BCBanHangTheoNgay[] temp = ChiTiet();
                BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, sotienphaithu, sotiendathu);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Tất Cả
        void XuLyTatCa()
        {
        }
        #endregion

        #region bán lẻ
        void XuLyBanLe()
        {
            // lấy hàng hóa trong kho
            if (cbbKho.SelectedIndex == khohang.Length)
            {
                hanghoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                foreach (Entities.GoiHang item in goihang)
                {
                    bool kt = false;
                    foreach (Entities.ChiTietGoiHang item1 in chitietgoihang)
                    {
                        if (item1.MaGoiHang.ToUpper() == item.MaGoiHang.ToUpper())
                        {
                            foreach (Entities.HangHoa item2 in hanghoa)
                            {
                                if (item2.MaHangHoa.ToUpper() == item1.MaHangHoa.ToUpper())
                                {
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt)
                                break;
                        }
                    }
                    if (kt)
                    {
                        Entities.HangHoa hh = new Entities.HangHoa();
                        hh.MaHangHoa = item.MaGoiHang;
                        hh.TenHangHoa = item.TenGoiHang;
                        hh.GiaBanBuon = item.GiaBanBuon;
                        hh.GiaBanLe = item.GiaBanLe;
                        hh.GiaNhap = item.GiaNhap;
                        hh.MaThue = "";
                        hh.MaDonViTinh = "";
                        array.Add(hh);
                    }
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            else
            {
                Entities.ChiTietKhoHang kh = new Entities.ChiTietKhoHang();
                kh.HanhDong = "HangHoaTheoKho";
                kh.MaKho = makho;
                hanghoa = Common.StaticVariable.InterfaceObject.Select_HangHoa(kh);
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                foreach (Entities.GoiHang item in goihang)
                {
                    bool kt = false;
                    foreach (Entities.ChiTietGoiHang item1 in chitietgoihang)
                    {
                        if (item1.MaGoiHang == item.MaGoiHang)
                        {
                            foreach (Entities.HangHoa item2 in hanghoa)
                            {
                                if (item2.MaHangHoa == item1.MaHangHoa)
                                {
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt)
                                break;
                        }
                    }
                    if (kt)
                    {
                        Entities.HangHoa hh = new Entities.HangHoa();
                        hh.MaHangHoa = item.MaGoiHang;
                        hh.TenHangHoa = item.TenGoiHang;
                        hh.GiaBanBuon = item.GiaBanBuon;
                        hh.GiaBanLe = item.GiaBanLe;
                        hh.GiaNhap = item.GiaNhap;
                        hh.MaThue = "";
                        hh.MaDonViTinh = "";
                        array.Add(hh);
                    }
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            double tongtien = 0;
            int sotang = 0;
            array = new ArrayList();
            array1 = new ArrayList();
            sotiendathu = sotienphaithu = 0;
            foreach (Entities.HangHoa ite in hanghoa)
            {
                string phantranchietkhau = "0";
                int soluong = 0;
                double tt = 0;
                string thue = "0";
                sotang = 0;
                foreach (Entities.HDBanLe item in hdbanle)
                {
                    if (cbbKho.SelectedIndex == khohang.Length)
                    {
                        if (item.NgayLap.Date == datesosanh.Date)
                        {
                            foreach (Entities.ChiTietHDBanle item1 in chitiethdbanle)
                            {
                                if (item1.MaHDBanLe == item.MaHDBanLe && ite.MaHangHoa == item1.MaHangHoa)
                                {
                                    soluong += item1.SoLuong;
                                    phantranchietkhau = item1.PhanTramChietKhau;
                                    thue = item1.ThueGTGT;
                                    if (double.Parse(item1.DonGia) != 0)
                                    {
                                        tt += double.Parse(item1.ThanhTien);
                                    }
                                }
                            }
                            if (soluong != 0)
                            {
                                bool kt = false;
                                foreach (Entities.HDBanLe item1 in (Entities.HDBanLe[])array.ToArray(typeof(Entities.HDBanLe)))
                                {
                                    if (item1.MaHDBanLe == item.MaHDBanLe)
                                    {
                                        kt = true;
                                        break;
                                    }
                                }
                                array.Add(item);
                                if (!kt)
                                {
                                    sotiendathu += double.Parse(item.TongTienThanhToan);
                                    tongtien += double.Parse(item.TongTienThanhToan);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (item.NgayLap.Date == datesosanh.Date && item.MaKho == makho)
                        {
                            foreach (Entities.ChiTietHDBanle item1 in chitiethdbanle)
                            {
                                if (item.MaHDBanLe == item1.MaHDBanLe && ite.MaHangHoa == item1.MaHangHoa)
                                {
                                    soluong += item1.SoLuong;
                                    phantranchietkhau = item1.PhanTramChietKhau;
                                    thue = item1.ThueGTGT;
                                    if (double.Parse(item1.DonGia) != 0)
                                    {
                                        tt += double.Parse(item1.ThanhTien);
                                    }
                                }
                            }
                            if (soluong != 0)
                            {
                                bool kt = false;
                                foreach (Entities.HDBanLe item1 in (Entities.HDBanLe[])array.ToArray(typeof(Entities.HDBanLe)))
                                {
                                    if (item1.MaHDBanLe == item.MaHDBanLe)
                                    {
                                        kt = true;
                                        break;
                                    }
                                }
                                array.Add(item);
                                if (!kt)
                                {
                                    sotiendathu += double.Parse(item.TongTienThanhToan);
                                    tongtien += double.Parse(item.TongTienThanhToan);
                                }
                            }
                        }
                    }

                }
                if (soluong != 0)
                {
                    Entities.BCBanHangTheoNgay temp = new Entities.BCBanHangTheoNgay();
                    temp.STT = (sotang + 1).ToString();
                    temp.DVT = DVT(ite.MaDonViTinh);
                    temp.DonGia = Common.Utilities.FormatMoney(ite.GiaBanLe);
                    temp.KhuyenMai = phantranchietkhau;
                    temp.MaHang = ite.MaHangHoa;
                    temp.SoLuong = soluong.ToString();
                    temp.TenHang = ite.TenHangHoa;
                    temp.TongTien = double.Parse(Common.Utilities.FormatMoney(tt.ToString()));
                    temp.GiaTriTT = Common.Utilities.FormatMoney(tt.ToString());
                    temp.Thue = thue;
                    array1.Add(temp);
                    sotang++;
                }

            }

            sotang = 0;
            hienthi = new Entities.BCBanHangTheoNgay[array1.Count + 3];
            Entities.BCBanHangTheoNgay[] te = (Entities.BCBanHangTheoNgay[])array1.ToArray(typeof(Entities.BCBanHangTheoNgay));

            foreach (Entities.BCBanHangTheoNgay item in te)
            {
                hienthi[sotang] = item;
                sotang++;
            }

            hienthi[hienthi.Length - 3] = new Entities.BCBanHangTheoNgay();
            hienthi[hienthi.Length - 3].DonGia = "Tổng tiền";
            hienthi[hienthi.Length - 3].GiaTriTT = (Common.Utilities.FormatMoney(tongtien.ToString()));
            hienthi[hienthi.Length - 2] = new Entities.BCBanHangTheoNgay();
            hienthi[hienthi.Length - 2].DonGia = "Tổng tiền đã thu";
            hienthi[hienthi.Length - 2].GiaTriTT = (Common.Utilities.FormatMoney(sotiendathu.ToString()));
            hienthi[hienthi.Length - 1] = new Entities.BCBanHangTheoNgay();
            hienthi[hienthi.Length - 1].DonGia = "Tổng tiền phải thu";
            hienthi[hienthi.Length - 1].GiaTriTT = (Common.Utilities.FormatMoney(sotienphaithu.ToString()));

            dgvHienThi.DataSource = hienthi; fix();
        }

        string DVT(string madonvitinh)
        {
            foreach (Entities.DVT item in dvt)
            {
                if (item.MaDonViTinh == madonvitinh)
                    return item.TenDonViTinh;
            }
            return "";
        }
        #endregion

        #region bán buôn
        void XuLyBanBuon()
        {
            // lấy hàng hóa trong kho
            if (cbbKho.SelectedIndex == khohang.Length)
            {
                hanghoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                foreach (Entities.GoiHang item in goihang)
                {
                    bool kt = false;
                    foreach (Entities.ChiTietGoiHang item1 in chitietgoihang)
                    {
                        if (item1.MaGoiHang.ToUpper() == item.MaGoiHang.ToUpper())
                        {
                            foreach (Entities.HangHoa item2 in hanghoa)
                            {
                                if (item2.MaHangHoa.ToUpper() == item1.MaHangHoa.ToUpper())
                                {
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt)
                                break;
                        }
                    }
                    if (kt)
                    {
                        Entities.HangHoa hh = new Entities.HangHoa();
                        hh.MaHangHoa = item.MaGoiHang;
                        hh.TenHangHoa = item.TenGoiHang;
                        hh.GiaBanBuon = item.GiaBanBuon;
                        hh.GiaBanLe = item.GiaBanLe;
                        hh.GiaNhap = item.GiaNhap;
                        hh.MaThue = "";
                        hh.MaDonViTinh = "";
                        array.Add(hh);
                    }
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            else
            {
                Entities.ChiTietKhoHang kh = new Entities.ChiTietKhoHang();
                kh.HanhDong = "HangHoaTheoKho";
                kh.MaKho = makho;
                hanghoa = Common.StaticVariable.InterfaceObject.Select_HangHoa(kh);
                if (hanghoa == null)
                    hanghoa = new Entities.HangHoa[0];
                goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                if (goihang == null)
                    goihang = new Entities.GoiHang[0];
                chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                if (chitietgoihang == null)
                    chitietgoihang = new Entities.ChiTietGoiHang[0];
                array = new ArrayList();
                foreach (Entities.HangHoa item in hanghoa)
                {
                    array.Add(item);
                }
                foreach (Entities.GoiHang item in goihang)
                {
                    bool kt = false;
                    foreach (Entities.ChiTietGoiHang item1 in chitietgoihang)
                    {
                        if (item1.MaGoiHang.ToUpper() == item.MaGoiHang.ToUpper())
                        {
                            foreach (Entities.HangHoa item2 in hanghoa)
                            {
                                if (item2.MaHangHoa.ToUpper() == item1.MaHangHoa.ToUpper())
                                {
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt)
                                break;
                        }
                    }
                    if (kt)
                    {
                        Entities.HangHoa hh = new Entities.HangHoa();
                        hh.MaHangHoa = item.MaGoiHang;
                        hh.TenHangHoa = item.TenGoiHang;
                        hh.GiaBanBuon = item.GiaBanBuon;
                        hh.GiaBanLe = item.GiaBanLe;
                        hh.GiaNhap = item.GiaNhap;
                        hh.MaThue = "";
                        hh.MaDonViTinh = "";
                        array.Add(hh);
                    }
                }
                hanghoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
            }
            double tongtien = 0;
            int sotang = 0;
            array = new ArrayList();
            array1 = new ArrayList();
            sotiendathu = sotienphaithu = 0;
            foreach (Entities.HangHoa ite in hanghoa)
            {
                string phantranchietkhau = "0";
                int soluong = 0;
                double tt = 0;
                string thue = "0";
                double cktm = 0;
                foreach (Entities.HDBanBuon item in hdbanbuon)
                {
                    if (cbbKho.SelectedIndex == khohang.Length)
                    {
                        if (item.NgayLap.Date == datesosanh.Date)
                        {
                            foreach (Entities.ChiTietHDBanBuon item1 in chitiethdbanbuon)
                            {
                                if (item1.MaHDBanBuon == item.MaHDBanBuon && ite.MaHangHoa.ToUpper() == item1.MaHangHoa.ToUpper())
                                {
                                    soluong += item1.SoLuong;
                                    phantranchietkhau = item1.PhanTramChietKhau;
                                    thue = item1.ThueGTGT;
                                    if (double.Parse(item1.DonGia) != 0)
                                    {
                                        tt += double.Parse(item1.ThanhTien);
                                        cktm += (double.Parse(item1.ThanhTien) * item.PhanTramChietKhauThuongmai / 100);
                                    }
                                }
                            }
                            if (soluong != 0)
                            {

                                bool kt = false;
                                foreach (Entities.HDBanBuon item1 in (Entities.HDBanBuon[])array.ToArray(typeof(Entities.HDBanBuon)))
                                {
                                    if (item1.MaHDBanBuon == item.MaHDBanBuon)
                                    {
                                        kt = true;
                                        break;
                                    }
                                }
                                array.Add(item);
                                if (!kt)
                                {
                                    sotiendathu += double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu);
                                    sotienphaithu += double.Parse(item.TongTienThanhToan) - (double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu));
                                    tongtien += double.Parse(item.TongTienThanhToan);
                                }
                            }

                        }
                    }
                    else
                    {
                        if (item.NgayLap.Date == datesosanh.Date && item.MaKho == makho)
                        {
                            foreach (Entities.ChiTietHDBanBuon item1 in chitiethdbanbuon)
                            {
                                if (item.MaHDBanBuon == item1.MaHDBanBuon && ite.MaHangHoa.ToUpper() == item1.MaHangHoa.ToUpper())
                                {
                                    soluong += item1.SoLuong;
                                    phantranchietkhau = item1.PhanTramChietKhau;
                                    thue = item1.ThueGTGT;
                                    if (double.Parse(item1.DonGia) != 0)
                                    {
                                        tt += double.Parse(item1.ThanhTien);
                                        cktm += (double.Parse(item1.ThanhTien) * item.PhanTramChietKhauThuongmai / 100);
                                    }
                                }
                            }
                            if (soluong != 0)
                            {
                                bool kt = false;
                                foreach (Entities.HDBanBuon item1 in (Entities.HDBanBuon[])array.ToArray(typeof(Entities.HDBanBuon)))
                                {
                                    if (item1.MaHDBanBuon == item.MaHDBanBuon)
                                    {
                                        kt = true;
                                        break;
                                    }
                                }
                                array.Add(item);
                                if (!kt)
                                {
                                    sotiendathu += double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu);
                                    sotienphaithu += double.Parse(item.TongTienThanhToan) - (double.Parse(item.ThanhToanNgay) + double.Parse(item.ThanhToanSauKhiLapPhieu));
                                    tongtien += double.Parse(item.TongTienThanhToan);
                                }
                            }
                        }

                    }
                }
                if (soluong != 0)
                {
                    Entities.BCBanHangTheoNgay temp = new Entities.BCBanHangTheoNgay();
                    temp.STT = (sotang + 1).ToString();
                    temp.DVT = DVT(ite.MaDonViTinh);
                    temp.DonGia = Common.Utilities.FormatMoney(ite.GiaBanBuon);
                    temp.KhuyenMai = phantranchietkhau;
                    temp.MaHang = ite.MaHangHoa.ToUpper();
                    temp.SoLuong = soluong.ToString();
                    temp.TenHang = ite.TenHangHoa;
                    temp.TongTien = double.Parse(Common.Utilities.FormatMoney(tt.ToString()));
                    temp.GiaTriTT = Common.Utilities.FormatMoney(tt.ToString());
                    temp.Thue = thue;
                    temp.CKTM = cktm;
                    array1.Add(temp);
                    sotang++;
                }
            }


            sotang = 0;
            hienthi = new Entities.BCBanHangTheoNgay[array1.Count + 3];
            Entities.BCBanHangTheoNgay[] te = (Entities.BCBanHangTheoNgay[])array1.ToArray(typeof(Entities.BCBanHangTheoNgay));

            foreach (Entities.BCBanHangTheoNgay item in te)
            {
                hienthi[sotang] = item;
                sotang++;
            }

            hienthi[hienthi.Length - 3] = new Entities.BCBanHangTheoNgay();
            hienthi[hienthi.Length - 3].DonGia = "Tổng tiền";
            hienthi[hienthi.Length - 3].GiaTriTT = (Common.Utilities.FormatMoney(tongtien.ToString()));
            hienthi[hienthi.Length - 2] = new Entities.BCBanHangTheoNgay();
            hienthi[hienthi.Length - 2].DonGia = "Tổng tiền đã thu";
            hienthi[hienthi.Length - 2].GiaTriTT = (Common.Utilities.FormatMoney(sotiendathu.ToString()));
            hienthi[hienthi.Length - 1] = new Entities.BCBanHangTheoNgay();
            hienthi[hienthi.Length - 1].DonGia = "Tổng tiền phải thu";
            hienthi[hienthi.Length - 1].GiaTriTT = (Common.Utilities.FormatMoney(sotienphaithu.ToString()));

            dgvHienThi.DataSource = hienthi; fix();
        }
        #endregion

        #region xử lý
        void XuLy()
        {
            if (makho == "")
            {
                return;
            }
            if (cbbLoai.SelectedIndex == 2)
            {
                XuLyTatCa();
            }
            if (cbbLoai.SelectedIndex == 1)
            {
                XuLyBanBuon();
            }
            if (cbbLoai.SelectedIndex == 0)
            {
                XuLyBanLe();
            }
            save = null;

        }
        #endregion

        #region Word
        void Word()
        {
            if (hienthi.Length - 3 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Word |*.doc"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCBanHangTheoNgay[] temp = ChiTiet();
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, sotienphaithu, sotiendathu, savefile.FileName, Common.Constants.Word);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Excel
        void Excel()
        {
            if (hienthi.Length - 3 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Excel |*.xls"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCBanHangTheoNgay[] temp = ChiTiet();
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, sotienphaithu, sotiendathu, savefile.FileName, Common.Constants.Excel);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region PDF
        void PDF()
        {
            if (hienthi.Length - 3 == 0)
            {
                return;
            }
            try
            {

                savefile.Filter = "PDF |*.pdf"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCBanHangTheoNgay[] temp = ChiTiet();
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, tenkho, loaihoadon, sotienphaithu, sotiendathu, savefile.FileName, Common.Constants.PDF);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        double sotiendathu = 0;
        double sotienphaithu = 0;
        Entities.BCBanHangTheoNgay[] ChiTiet()
        {
            array = new ArrayList();
            foreach (Entities.BCBanHangTheoNgay item in hienthi)
            {
                if (item.MaHang != null)
                {
                    array.Add(item);
                }
            }
            return (Entities.BCBanHangTheoNgay[])array.ToArray(typeof(Entities.BCBanHangTheoNgay));
        }

        private void frmBanHangTheoNgay_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
            }
            catch
            {
            }
        }

        private void cbbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaihoadon = cbbLoai.Text;
            XuLy();
        }

        private void cbbKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbKho.SelectedIndex < 0)
                return;
            makho = cbbKho.SelectedValue.ToString();
            tenkho = cbbKho.Text;
            XuLy();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            datesosanh = DateTime.Parse(dateTimePicker1.Value.ToString("MM/dd/yyyy"));
            XuLy();
        }

        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                i = e.RowIndex;
                save = ((Entities.BCBanHangTheoNgay[])dgvHienThi.DataSource)[i];
            }

        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            In();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnWord_Click(object sender, EventArgs e)
        {
            Word();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            PDF();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
    }
}
